package plus.H5D3B5016.view;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import plus.H5D3B5016.R;
import plus.H5D3B5016.utils.ACache;

public class TxDialog extends DialogFragment {

    private Context   mContext;
    private View      frView;
    private int type=0;

    EditText st;
    EditText et;
    TextView t1;
    TextView t2;
    Button ok;

    public TxDialog(Context context){
        this.mContext=context;
    }

    public TxDialog(Context context,int type){
        this.mContext=context;
        this.type=type;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        frView = inflater.inflate(R.layout.txdialog, null);
        st=frView.findViewById(R.id.st);
        et=frView.findViewById(R.id.et);
        ok=frView.findViewById(R.id.ok);
        t1=frView.findViewById(R.id.t1);
        t2=frView.findViewById(R.id.t2);
        String txst="30";
        String txet="30";

        if(type==1){
            t1.setText("内自动下单");
            t2.setText("自动平仓");
            txst= ACache.get(mContext).getAsString("txst2");
            txet=ACache.get(mContext).getAsString("txet2");
        }else{
            t1.setText("秒内价格浮动相差");
            t2.setText("响铃提醒");
            txst= ACache.get(mContext).getAsString("txst");
            txet=ACache.get(mContext).getAsString("txet");
        }
        int sts=30;
        int ets=30;
        if(null!=txst){
            sts=Integer.valueOf(txst);
        }
        if(null!=txet){
            ets=Integer.valueOf(txet);
        }
        st.setText(""+sts);
        et.setText(""+ets);

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(st.getText().toString().equals("")||et.getText().toString().equals("")){
                    Toast.makeText(mContext,"不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                int sts=Integer.valueOf(st.getText().toString());
                int ets=Integer.valueOf(et.getText().toString());
                if(sts<10&&type==0){
                    Toast.makeText(mContext,"时间不能太小",Toast.LENGTH_SHORT).show();return;
                }
                if(ets<1){
                    Toast.makeText(mContext,"金额不能太小",Toast.LENGTH_SHORT).show();return;
                }

                if(null!=mOnConfirmClickListener){
                    mOnConfirmClickListener.onConfirm(sts,ets);
                }
                dismiss();
            }
        });

        return frView;
    }


    public  interface OnConfirmClickListener {
        void onConfirm(int st, int et);
    }

    private OnConfirmClickListener mOnConfirmClickListener;
    public void setOnConfirmClickListener(OnConfirmClickListener listener) {
        mOnConfirmClickListener = listener;
    }

}
